package fr.esiea.es_inf5042.gui;

import java.util.List;

import fr.esiea.es_inf5042.MainActivity;
import fr.esiea.es_inf5042.R;
import fr.esiea.es_inf5042.business.Bargain;
import fr.esiea.es_inf5042.business.User;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class UserDialog extends Dialog{

	private Context context;
	private MainActivity main;
	
	private EditText editTextName;
	
	private User user;
	
	public UserDialog(Context context) {
		super(context);
		this.context = context;
		this.main = (MainActivity) context;
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_dialog);
		setTitle(R.string.title_user_dialog);
		setCancelable(true);
		
		// set up Name
		editTextName = (EditText) findViewById(R.id.EditTextName);
		if (user.getName() != null)
			editTextName.setText(user.getName());			
		
		// set up BargainList
		ListView bargainList = (ListView) findViewById(R.id.list_bargains);
		ArrayAdapter<String> bargainAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);
				
		List<Bargain> bargains = null;
				
		bargains = main.getAllBargainsFromUser(user);
				
		if (bargains != null)
		{
			for (Bargain bargain : bargains)
				bargainAdapter.add(bargain.getTitle());
		}
					
		else
			bargainAdapter.add("You don't have any bargain");
			
		bargainList.setAdapter(bargainAdapter);
		bargainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					BargainDialog bargainDialog = new BargainDialog(context, parent.getItemAtPosition(position).toString());
					bargainDialog.show();
			}  
		});
		
		// set up FriendList
		ListView friendList = (ListView) findViewById(R.id.list_friends);
		ArrayAdapter<String> friendAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);
		if (user.getFriendsArrayList() != null)
			friendAdapter.addAll(user.getFriendsArrayList());
		else
			friendAdapter.add("You don't have any friend");
		friendList.setAdapter(friendAdapter);
		
		// set up Button AddFriends
		Button  buttonAddFriends = (Button) findViewById(R.id.ButtonAddFriends);
		buttonAddFriends.setOnClickListener(new View.OnClickListener() {
			public void onClick(View  v) {
				displayToast("Not done yet");
		    }
		});

		// set up Button SeeFriends
		Button  buttonSeeFriends = (Button) findViewById(R.id.ButtonSeeFriends);
		buttonSeeFriends.setOnClickListener(new View.OnClickListener() {
			public void onClick(View  v) {
				displayToast("Not done yet");
		    }
		});
		
		// set up Button OK
		Button  buttonOk = (Button) findViewById(R.id.ButtonOk);
		buttonOk.setOnClickListener(new buttonOkListener());
		
		// set up Button CANCEL
		Button  buttonCancel = (Button) findViewById(R.id.ButtonCancel);
		buttonCancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View  v) {
		        if (!user.getName().equals(""))
		        	dismiss();
		        else
		        	displayToast("The field \"name\" must be filled");
		    }
		});

	}
	
	public void setUser(User user) {
		this.user = user;
	}

	private void displayToast(String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}
	
	private class buttonOkListener implements View.OnClickListener {
		public void onClick(View v) {
			String name = editTextName.getText().toString();
			try {
				if (name.equals(user.getName()))
					dismiss();
				else if (!name.equals("")) {
					user.setName(name);
					displayToast("User modified");
					dismiss();
				}
				else{
					displayToast("The field \"name\" must be filled");
				}
					
			} catch (Exception e) {
				displayToast("ERROR - User not modified");
			}
		}
	}

}
